package com.example.shushiyunsst.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.shushiyunsst.R;
import com.warkiz.widget.IndicatorSeekBar;
import com.warkiz.widget.OnSeekChangeListener;
import com.warkiz.widget.SeekParams;

import java.text.MessageFormat;

public class ShuSiLiuIndicatorSeekBar extends LinearLayout implements OnSeekChangeListener {

    private IndicatorSeekBar mTransactionIndicatorSeekBar, mTransactionIndicatorSeekBarRed;

    private TextView progressTv;

    private OnSeekChangeListener mSeekChangeListener;

    public void setSeekChangeListener(OnSeekChangeListener mSeekChangeListener) {
        this.mSeekChangeListener = mSeekChangeListener;
    }

    public ShuSiLiuIndicatorSeekBar(Context context) {
        this(context, null);
    }

    public ShuSiLiuIndicatorSeekBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShuSiLiuIndicatorSeekBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        inflate(context, R.layout.shu_si_liu_indicator_seek_bar_layout, this);


        mTransactionIndicatorSeekBar = findViewById(R.id.mTransactionIndicatorSeekBar);
        mTransactionIndicatorSeekBarRed = findViewById(R.id.mTransactionIndicatorSeekBarRed);

        progressTv = findViewById(R.id.progressTv);

        mTransactionIndicatorSeekBar.setIndicatorTextFormat("${PROGRESS} %");
        mTransactionIndicatorSeekBar.setOnSeekChangeListener(this);

        mTransactionIndicatorSeekBarRed.setIndicatorTextFormat("${PROGRESS} %");
        mTransactionIndicatorSeekBarRed.setOnSeekChangeListener(this);
    }


    public void setProgress(float progress){
        mTransactionIndicatorSeekBar.setProgress(progress);
        mTransactionIndicatorSeekBarRed.setProgress(progress);
    }

    @Override
    public void onSeeking(SeekParams seekParams) {

        int progress = seekParams.progress;

        float min = indicatorMin();

        int paddingLeft = indicatorPaddingLeft();

        int width = progressTv.getWidth() / 2;

        float translationX = (progress - min) * getSeekLength() / getAmplitude() + paddingLeft - width;

        progressTv.setTranslationX(translationX);

        progressTv.setText(MessageFormat.format("{0}%", progress));
    }

    private float indicatorMax() {
        return isSelected() ?
                mTransactionIndicatorSeekBarRed.getMax()
                : mTransactionIndicatorSeekBar.getMax();
    }

    private float indicatorMin() {
        return isSelected() ?
                mTransactionIndicatorSeekBarRed.getMin()
                : mTransactionIndicatorSeekBar.getMin();
    }


    private int indicatorPaddingLeft() {
        return isSelected() ?
                mTransactionIndicatorSeekBarRed.getPaddingLeft()
                : mTransactionIndicatorSeekBar.getPaddingLeft();
    }

    private int indicatorPaddingRight() {
        return isSelected() ?
                mTransactionIndicatorSeekBarRed.getPaddingRight()
                : mTransactionIndicatorSeekBar.getPaddingRight();
    }

    private int indicatorWidth() {
        return isSelected() ?
                mTransactionIndicatorSeekBarRed.getWidth()
                : mTransactionIndicatorSeekBar.getWidth();
    }

    private int getSeekLength() {

        int width = indicatorWidth();

        int paddingLeft = indicatorPaddingLeft();

        int paddingRight = indicatorPaddingRight();

        return width - paddingLeft - paddingRight;
    }

    private float getAmplitude() {

        float max = indicatorMax();

        float min = indicatorMin();

        return (max - min) > 0 ? (max - min) : 1;
    }

    @Override
    public void onStartTrackingTouch(IndicatorSeekBar seekBar) {
//
        progressTv.setVisibility(VISIBLE);
//
        progressTv.setText(MessageFormat.format("{0}%", seekBar.getProgress()));

        if (mSeekChangeListener != null) {
            mSeekChangeListener.onStartTrackingTouch(seekBar);
        }
    }

    @Override
    public void onStopTrackingTouch(IndicatorSeekBar seekBar) {
        if (seekBar.getProgress() <= 0) {
            progressTv.setVisibility(INVISIBLE);
        } else {
            progressTv.setVisibility(VISIBLE);
        }
        if (mSeekChangeListener != null) {
            mSeekChangeListener.onStopTrackingTouch(seekBar);
        }
    }


    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            mTransactionIndicatorSeekBarRed.setVisibility(VISIBLE);
            mTransactionIndicatorSeekBar.setVisibility(GONE);

            mTransactionIndicatorSeekBarRed.setProgress(mTransactionIndicatorSeekBar.getProgress());
        } else {
            mTransactionIndicatorSeekBarRed.setVisibility(GONE);
            mTransactionIndicatorSeekBar.setVisibility(VISIBLE);

            mTransactionIndicatorSeekBar.setProgress(mTransactionIndicatorSeekBarRed.getProgress());
//
        }
    }


    public float getProgress(){
        if (isSelected()){
           return mTransactionIndicatorSeekBarRed.getProgress();
        }else {
            return mTransactionIndicatorSeekBar.getProgress();
        }
    }
}
